package cn.xy.basic.controller;

import cn.xy.basic.dto.RegisterDto;
import cn.xy.basic.exception.BusinessException;
import cn.xy.basic.service.IVerifyCodeService;
import cn.xy.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 抽取接口工具类
 * 图形验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 图形验证码
     *
     * @param key
     * @return
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String key) {
        try {
            String base64Str = verifyCodeService.imageCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("业务繁忙");
        }

    }

    /**
     * 短信验证码
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto) {
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("业务繁忙");
        }


    }
}